package com.hpeng.sys.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.hpeng.sys.model.AssetCard;
import com.hpeng.sys.model.AssetCardBook;
import com.hpeng.sys.model.SysUser;
import com.hpeng.sys.service.AssetCardBooksMgr;
import com.hpeng.sys.service.AssetCardsMgr;
import com.hpeng.sys.service.LoginUserMgr;
import com.hpeng.sys.service.SysUserMgr;
import com.hpeng.sys.vo.LoginInfo;
import com.hpeng.util.MD5;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Component("LoginAction")
@Scope("prototype")
public class LoginAction extends ActionSupport implements ModelDriven {

	private LoginInfo info = new LoginInfo();
	private SysUser user;
	private List<AssetCard> acds;
	private List<AssetCardBook> acdbs;
	public String login() {
		user = sysUserMgr.getUser(info.getUserId());
		if (user != null) {
			if (user.getPassword().equals(MD5.toMD5(info.getPassword()))) {
				Map<String, Object> sMap = ActionContext.getContext()
						.getSession();
				sMap.put("user", user);
				return "success";
			}
		}
		addActionMessage(getText("login.error"));
		return "fail";
	}

	public String logout(){
		ActionContext.getContext().getSession().remove("user");
		return "logout";
	}
	public String showAsk(){
		user = sysUserMgr.getUser(loginUserMgr.getUserId(ServletActionContext.getRequest()));
		System.out.println(user);
		acds = assetCardsMgr.getByDepAndStatus(user.getStaid(),user.getTemplateCode());
		acdbs = assetCardBooksMgr.getByAssetCardList(acds);
		return "SUCCESS";
	}
	public String chgPwd(){
		return "toChgPwd";
	}

	public Object getModel() {
		return info;
	}

	public LoginInfo getInfo() {
		return info;
	}

	public void setInfo(LoginInfo info) {
		this.info = info;
	}
	
	public SysUser getUser() {
		return user;
	}

	public void setUser(SysUser user) {
		this.user = user;
	}
	public void setAcds(List<AssetCard> acds) {
		this.acds = acds;
	}
	public List<AssetCard> getAcds() {
		return acds;
	}
	public void setAcdbs(List<AssetCardBook> acdbs) {
		this.acdbs = acdbs;
	}

	public List<AssetCardBook> getAcdbs() {
		return acdbs;
	}
	@Resource
	private AssetCardBooksMgr assetCardBooksMgr;
	@Resource
	private LoginUserMgr loginUserMgr;
	@Resource
	private SysUserMgr sysUserMgr;
	@Resource
	private AssetCardsMgr assetCardsMgr;
}